package com.fengye.algorithms.leecode;

import com.fengye.algorithms.leecode.common.TreeNode;

/**
 * @Author fengye
 * @Date 2023/4/20 9:11
 **/
public class LowestCommonAncestorOfABinarySearchTree235 {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // root比p和q都大，那么是在左子树上
        if(root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.left, p, q);
        }
        // root比p和q都小，那么是在右子树上
        if(root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }

        return root;
    }
}
